/*
* @Author: chenjianghui
* @Date:   2018-09-04 15:19:02
* @Last Modified by:   chenjianghui
* @Last Modified time: 2018-09-05 19:19:08
*/
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/shop/index',
                    add_url: 'shop/shop/add',
                    edit_url: 'shop/shop/edit',
                    del_url: 'shop/shop/del',
                    multi_url: 'shop/shop/multi',
                    table: 'shop',
                }
            });

            var table = $("#table");

            //在表格内容渲染完成后回调的事件
            table.on('post-body.bs.table', function (e, json) {
                $("tbody tr[data-index]", this).each(function () {
                    // if (parseInt($("td:eq(1)", this).text()) == Config.admin.id) {
                    //     $("input[type=checkbox]", this).prop("disabled", true);
                    // }
                });
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'shop_id',
                sortName: 'shop_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'shop_id', title: __('Shop_id')},
                        {field: 'shop_name', title: __('Shop_name')},
                        {field: 'mobile', title: __('Mobile')},
                        {field: 'consignor', title: __('Consignor')},
                        {field: 'area_id', title: __('Area_id')},
                        {field: 'wanwan_id', title: __('Wanwan_id')},
                        {field: 'shop_type', title: __('Shop_type')},
                        {field: 'status', title: __('Status')},
                        {field: 'shop_cat', title: __('Shop_cat')},
                        {field: 'shop_url', title: __('Shop_url'),},
                        {field: 'shop_pic', title: __('Shop_pic'), formatter: Controller.api.formatter.thumb, operate: false},
                       // {field: 'shop_admin_id', title: __('Shop_admin_id')},
                        {field: 'limit_num', title: __('单数限制')},
                        {field: 'left_num', title: __('剩余单数')},
                        {field: 'gold_bean', title: __('隔日单金豆数量'), operate:'BETWEEN'},
                        {field: 'jing_bean', title: __('精刷单金豆数量'), operate:'BETWEEN'},
                        // {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'delete_time', title: __('Delete_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,events: Table.api.events.operate, formatter: function (value, row, index) {
                                console.log(row);
                                $(table).data("operate-edit", null);
                                return Table.api.formatter.operate.call(this, value, row, index);
                            }}
                    ]
                ]
            });



            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        pass: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                thumb: function (value, row, index) {
                    //if (row.mimetype.indexOf("image") > -1) {
                        var style = row.storage == 'upyun' ? '!/fwfh/120x90' : '';
                        return '<a href="' + row.fullurl + '" target="_blank"><img src="' + row.fullurl + style + '" alt="" style="max-height:90px;max-width:120px"></a>';
                    // } else {
                    //     return '<a href="' + row.fullurl + '" target="_blank">' + __('None') + '</a>';
                    // }
                },
                url: function (value, row, index) {
                    return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + value + '</a>';
                },
            }
        }
    };
    return Controller;
});